<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('采购申请头文件')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
    <style type="text/css">
        .la {
            text-align: left;
        }

        .textBox {
            text-align: right;
        }
    </style>
</head>

<body class="gray-bg">

<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="inquiryQuotationForm" th:action="@{/parity/list}" class="form-inline">
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3">
                            <label>汇总号：</label>
                            <input id="inquiryTotalCode" name="inquiryTotalCode" type="text"/>
                        </li>
                        <li class="col-sm-3">
                            <label>物料编码：</label>
                            <input id="materialCode" name="materialCode" type="text"/>
                        </li>
                        <li class="col-sm-3">
                            <label>询价报价单号：</label>
                            <input id="inquiryCode" name="inquiryCode" type="text"/>
                        </li>
                        <li class="col-sm-3">
                            <button id="inquiryQuotationSearchBtn" type="button"
                                    class="btn btn-primary btn-rounded btn-sm"><i class="fa fa-search"></i>&nbsp;搜索
                            </button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置
                            </button>
                            <button id="inquiryQuotationSearch" type="button" class="btn btn-primary btn-rounded btn-sm"
                                    style="display: none"><i class="fa fa-caret-down"></i>&nbsp;高级搜索
                            </button>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-info order-table">
            <!-- <div class="btn-group hidden-xs toolbar" id="parityToolBar" role="group"> -->
            <div class="btn-group hidden-xs toolbar" id="inquiryQuotationToolBar" role="group">
                <a class="btn btn-outline btn-success btn-rounded" data-toggle='modal' onclick="show()">
                    <i class="fa fa-plus"></i> 修改
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="Update()">
                <i class="fa fa-edit"></i> 更新记录维护
                </a>
                <a class="btn btn-outline btn-success btn-rounded" data-toggle='modal'  onclick="addPrice()">
                    <i class="fa fa-plus"></i> 议价金额
                </a>

                <a class="btn btn-outline btn-success btn-rounded" onclick="toOa()">
                    <i class="fa fa-edit"></i> 导入OA
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="Export()">
                    <i class="fa fa-edit"></i> 导出
                </a>
                <a class="btn btn-outline btn-danger btn-rounded" onclick="deleteInquiryId()">
                <i class="fa fa-edit"></i>删除
                </a>
            </div>
            <div style="height:360px; overflow:auto;">
                <table id="inquiryQuotation" data-mobile-responsive="true"></table>
            </div>
        </div>
    </div>
</div>
<!-- Modal -->
<!-- 模态框（Modal） -->
<div id="myModal" style="display: none">
    <div class="modal-body">
                    <input id="id" name="id" type="hidden" value=""/>
                    <div class="row">
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">汇总号</span>
                                <input name="inquiryTotalCode" type="text" id="inquiryTotalCodeid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">报价单号</span>
                                <input name="inquiryCode" type="text" id="inquiryCodeid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">公司代码</span>
                                <input name="companyCode" type="text" id="companyCodeid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">采购组织</span>
                                <input name="purchaseOrg" type="text" id="purchaseOrgid" class="form-control">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">税代码</span>
                                <input name="taxCode" type="text" id="taxCodeId"
                                       class="form-control" autocomplete="off">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">语言编码</span>
                                <input name="language" type="text" id="languageid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">货币</span>
                                <input name="currency" type="text" id="currencyid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">询价开始日期</span>
                                <input name="beginDate" type="text" id="beginDateid" class="form-control">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">报价截止日期</span>
                                <input name="endDate" type="text" id="endDateid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">供应商编码</span>
                                <input name="vendorCode" type="text" id="vendorCodeid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">供应商名称</span>
                                <input name="vendorName" type="text" id="vendorNameid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">付款条款</span>
                                <input name="payTermsPurchase" type="text" id="payTermsPurchaseid" class="form-control">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">行项目</span>
                                <input name="inquiryItemCode" type="text" id="inquiryItemCodeid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">物料编码</span>
                                <input name="companyCode" type="text" id="companyCodeid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">物料描述</span>
                                <input disabled name="materialDesc" type="text" id="materialDescid"
                                       class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">数量</span>
                                <input name="purchaseCnt" type="text" id="purchaseCntid" class="form-control">
                                <!--  <input name="quantity" type="text" id="quantity" class="form-control">-->
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">采购订单单位</span>
                                <input name="poUnit" type="text" id="poUnitid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">工厂</span>
                                <input name="werks" type="text" id="werksid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">物料组</span>
                                <input name="materialGroup" type="text" id="materialGroupid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">参考行项目</span>
                                <input name="purchRequestItemCode" type="text" id="purchRequestItemCodeid"
                                       class="form-control">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">报价</span>
                                <input name="price" type="text" id="priceid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">最小起订量</span>
                                <input name="priceUnit" type="text" id="priceUnitid" class="form-control">
                            </div>
                        </div>
                        <div class="form-group col-lg-3">
                            <div class="input-group">
                                <span class="input-group-addon">回复交期</span>
                                <input name="replyDeliveryDate" type="text" id="replyDeliveryDateId"
                                       class="form-control" autocomplete="off">
                            </div>
                        </div>
                    </div>
             </div>
                    <div class="modal-footer" style="margin-top:23px">
                        <button type="button" class="btn btn-default" onclick='closeDialog()'>关闭</button>
                        <button type="button" class="btn btn-primary" onclick='updateAll()'>确定</button>
                    </div>
</div><!-- /.modal-dialog -->
<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<!-- Page-Level Scripts -->

<!--添加议价金额-->
<div id="addEnquiryPriceModal" style="display: none">
    <div class="modal-body">
        <div style="margin-top:20px">
            <label><b>第一次议价：</b></label>
            <input type="number" id="firstPrice" class="form-control">
        </div>
        <div style="margin-top:20px">
            <label><b>第二次议价：</b></label>
            <input type="number" id="secondPrice" class="form-control">
        </div>
        <div style="margin-top:20px">
            <label><b>最终价：</b></label>
            <input type="number" id="lastPrice" class="form-control">
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" onclick='closeDialog()'>关闭</button>
        <button type="button" class="btn btn-primary" onclick='updateEnquiryPrice()'>确定</button>
    </div>
</div>


<!--  -->
<script type="text/javascript">
    //面向对象
    var searcherBS = Searcher.createBS('#inquiryQuotation',
        [
            {checkbox: true},
            {title: "询价报价单号", field: "inquiryCode"},
            {
                title: "状态 ", field: "state",
                formatter: function (value, row, index) {
                    if (value == 0) {
                        return "已报价未提交";
                    } else if (value == -1) {
                        return "报价单草稿";
                    } else if (value == 1) {
                        return "报价单已提交";
                    } else if (value == 2) {
                        return "报价单审核中";
                    } else if (value == 3) {
                        return "审核通过";
                    } else if (value == -3) {
                        return "审核驳回";
                    }else if (value == 4) {
                        return "新增成功";
                    }else if (value == 5) {
                        return "更改成功";
                    }
                }
            },
            {title: "采购单号 ", field: "requestCode"},
            {title: "汇总号", field: "inquiryTotalCode"},
            {title: "物料描述", field: "materialDesc"},
            {title: "物料编码", field: "materialCode"},
            {title: "数量", field: "purchaseCnt"},
            {title: "含税价", field: "price"},
            {title: "最小起订量", field: "priceUnit"},
            {title: "订单单位", field: "poPriceUnit"},
            {title: "税代码", field: "taxCode"},
            {title: "总价", field: "totalPrice"},
            {title: "回复交期", field: "replyDeliveryDate"},
            {title: "供应商编码", field: "vendorCode"},
            {title: "供应商名称", field: "vendorName"},
            {title: "第一次仪价",field:"firstEnquiryPrice"},
            {title: "第二次仪价",field:"secondEnquiryPrice"},
            {title: "最终价",field:"lastEnquiryPrice"}
        ], {pageList: [5, 10, 15, 50, 100], pageSize: 5}
    );

    //刷新数据
    function refresh() {
        searcherBS.refresh();
    }

    //弹框取消
    function closeDialog(){
        layer.closeAll();
    }

    //初始化所有字典数据
    initDictDataSel();
    // 初始化时间插件
    laydate.render({elem: '#replyDeliveryDateId'});


    $("#quotationId").val(searcherBS.getSelectedRows("quotationId"));

    /*
       * 修改功能
       * */
    function show() {
        var rowDatas = searcherBS.getSelectedRows("quotationId");
        if (rowDatas.length != 1) {
            Dialog.errorMsg("请选择一行进行修改！")
            return;
        }
        Dialog.open("详情", $("#myModal"), {area: ['1100px', '500px']});
        Page.ajaxPOST(ctxPath + "/parity/findById", {"quotationId": rowDatas}, function (res) {
            var data = JSON.stringify(res.result)
            //json字符串转换成json对象
            var jsonobj = eval('(' + data + ')');
            //获取要循环内的id 内
            getMyModal(true, jsonobj);
        });
    }
    /*
    * 通过传入状态判断是否获取值还是传入值
    * */
    var data = null;

    function getMyModal(flg, jsonobj) {
        //获取要循环内的id 内
        var itemEntity = {};
        var inputs = $("#myModal");
        inputs.each(function (index, input) {
            var its = $(input).find("input");
            its.each(function (idx, it) {
                var inputId = $(it).attr("name");
                if (flg == true) {
                    $(it).val(jsonobj[inputId])
                } else {
                    itemEntity[inputId] = $(it).val();
                }
            })
        });
        if (flg == false) {
            data = itemEntity;
        }
    }



    $(function () {
        $('#myModal').on('hide.bs.modal',
            function () {
                // alert('嘿，我听说您喜欢模态框...');
            })
    });

    /**
     * 添加议价金额
     */
    function addPrice(){
        var row = searcherBS.getRawData();
        if (row.length != 1) {
            Dialog.errorMsg("请选择一行添加议价金额！");
            return;
        }
        //询价窗口
        Dialog.open("询价", $("#addEnquiryPriceModal"), {area: ['500px', '480px']});

        //清除表单内容
        $("#firstPrice").val("");
        $("#secondPrice").val("");
        $("#lastPrice").val("");
    }

    /**
     * 更新议价金额
     */
    function updateEnquiryPrice() {
        var firstPrice=$("#firstPrice").val();
        var secondPrice=$("#secondPrice").val();
        var lastPrice=$("#lastPrice").val();
        var quotationId=searcherBS.getSelectedRows("quotationId");
        if(!lastPrice){
            Dialog.errorMsg("请填写最终价！");
            return;
        }
        data={"firstPrice":firstPrice,"secondPrice":secondPrice,"lastPrice":lastPrice,"quotationId":quotationId[0]};
        layer.load(1,{shade: [0.3, '#393D49']});
        $.ajax({
            url: ctxPath + "/parity/updateEnquiryPrice",
            type: 'POST',
            data:{"firstPrice":firstPrice,"secondPrice":secondPrice,"lastPrice":lastPrice,"quotationId":quotationId[0]},
            dataType: 'JSON',
            cache: false, // 不缓存
            success: function (data) {
                layer.closeAll();
                if (data.result) {
                    Dialog.msg("操作成功!");
                } else {
                    Dialog.errorMsg("操作失败，请重试!");
                }
                searcherBS.refresh();
            }
        });
    }

    /**
     * 将比价数据传输至OA审批
     */
    function toOa() {
        var rowDatas = searcherBS.getSelectedRows("quotationId");
        var rows = $("#inquiryQuotation").bootstrapTable('getSelections');
        if (rowDatas.length < 1) {
            Dialog.errorMsg("请至少选择一行");
            return;
        }
        /**
         数组 ：js判断一个数组是否包含一个指定的值
         1：array.indexOf   此方法判断数组中是否存在某个值，如果存在返回数组元素的下标，否则返回-1
         2： array.includes(searchElement[, fromIndex])   此方法判断数组中是否存在某个值，如果存在返回 true，否则返回false。
         3. array.find(callback[, thisArg])     返回数组中满足条件的第一个元素的值，如果没有，返回undefined

         */
        for (var i = 0; i < rows.length; i++) {
            if(rows[i].state!=1){
                Dialog.errorMsg("该状态不能比价！");
                return;
            }
            if (rows.length > 1) {
                if (rows.indexOf("-1") > -1 || rows.indexOf("2") > -1 || rows.indexOf("3") > -1 || rows.indexOf("-3") > -1 || rows.indexOf("0") > -1) {//则不包含该元素
                    Dialog.alert("此状态不允许审批!")
                    return;
                } else {
                    if (rows[i].state == 1) {
                        const set = new Set();
                        //遍历选中的值
                        for (var i = 0; i < rows.length; i++) {
                            set.add(rows[i].materialCode);
                        }
                        if (set.size == 1) {
                            var quotationId = rowDatas;
                            Dialog.confirm('确定要提交SAP审批？', function (index) {
                                Page.ajaxPOST(ctxPath + "/parity/toOa/" + quotationId, null, function () {
                                    Dialog.msg("操作成功", function () {
                                        searcherBS.refresh();
                                    });
                                }, {loadingMsg: '正在向OA系统发送数据，请稍候…'});
                            });
                        } else {
                            Dialog.alert("请选择相同物料号进行比价");
                        }

                    }
                }

            } else {
                if (rows[i].state == 1) {
                    const set = new Set();
                    //遍历选中的值
                    for (var i = 0; i < rows.length; i++) {
                        set.add(rows[i].materialCode);
                    }
                    if (set.size == 1) {
                        var quotationId = rowDatas;
                        Dialog.confirm('确定要提交OA审批？', function (index) {
                            Page.ajaxPOST(ctxPath + "/parity/toOa/" + quotationId, null, function () {
                                Dialog.msg("操作成功", function () {
                                    searcherBS.refresh();
                                });
                            }, {loadingMsg: '正在向OA系统发送数据，请稍候…'});
                        });
                    } else {
                        Dialog.alert("请选择相同物料号进行比价");
                    }

                } else {
                    if (rows[i].state == -1) {
                        Dialog.alert("供应商未报价");
                    }
                    if (rows[i].state == 2) {
                        Dialog.alert("报价单审核中");
                    }
                    if (rows[i].state == 3) {
                        Dialog.alert("审核通过");
                    }
                    if (rows[i].state == -3) {
                        Dialog.alert("审核驳回");
                    }
                    if (rows[i].state == 0) {
                        Dialog.alert("供应商以报价未确认");
                    }
                }
            }

            /*if(rows[i].state==1){
                const set = new Set();
                //遍历选中的值
                for (var i=0;i<rows.length;i++){
                    set.add(rows[i].materialCode);
                }
                if(set.size==1){
                        var quotationId = rowDatas;
                        Dialog.confirm('确定要提交SAP审批？', function(index){
                            Page.ajaxPOST(ctxPath + "/parity/toOa/" + quotationId, null, function () {
                                Dialog.msg("操作成功", function () {
                                    searcherBS.refresh();
                                });
                            }, {loadingMsg : '正在向OA系统发送数据，请稍候…'});
                        });
                }else{
                     Dialog.alert("请选择相同物料号进行比价");
                }

            }else{
                if(rows[i].state==-1){
                    Dialog.alert("供应商未报价");
                }
                if(rows[i].state==2){
                    Dialog.alert("报价单审核中");
                }
                if(rows[i].state==3){
                    Dialog.alert("审核通过");
                }
                if(rows[i].state==-3){
                    Dialog.alert("审核驳回");
                }
                if(rows[i].state==0){
                    Dialog.alert("供应商以报价未确认");
                }
            }*/
        }
        /*************************************************/
    }

    //报价单导出
    function Export() {
        //得到报价单id
        var quotationId = searcherBS.getSelectedRows("quotationId");
        if (quotationId != null && quotationId != "") {
            window.location.href = ctxPath + "/inquiryQuotation/Export?quotationId=" + quotationId;
        } else {
            Dialog.alert("请先选择要导出的报价单ID!!!");
        }
    }

    /*
   * 删除功能
   *
   * */
    function deleteInquiryId() {
        var row = searcherBS.getSelectedRows("quotationId");
        if (row.length < 1) {
            Dialog.errorMsg("至少选择一行删除！");
            return;
        }
        Dialog.confirm('确定删除？', function () {
            Page.ajaxPOST(ctxPath + "/parity/deleteParity", {quotationId: row}, function () {
                Dialog.msg("操作成功", function () {
                    searcherBS.refresh();
                });
            }, {"loadingMsg": "正往服务器提交数据......"});
        });
    }
    //更新信息记录维护
    function Update() {
        var quotationId = searcherBS.getSelectedRows("quotationId");
        if (quotationId.length < 1) {
            Dialog.errorMsg("请至少选择一行");
            return;
        }
        var rows = $("#inquiryQuotation").bootstrapTable('getSelections');
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].state == 3) {
                Page.ajaxPOST(ctxPath + "/inquiryQuotation/updateSouce/"+rows[i].quotationId, null, function (res) {
                    Dialog.msg("更新成功", function () {
                        searcherBS.refresh();
                        // parent.searchDept();
                        // Dialog.closeFrame(window.name);
                    })
                })
            }else if(rows[i].state == 4||rows[i].state == 5) {
                Dialog.errorMsg("该条数据已经更新过信息记录,不允许再次更新!");
                return;
            }else{
                Dialog.errorMsg("所选数据不是审核通过状态,无法更新");
                return;
            }

        }
    }
    //比价查看modal更新数据
    function updateQuotation() {
        var row=searcherBS.getRawData();
        if(row[0].state!=-1){
            Dialog.errorMsg("非草稿状态不可修改！");
            return;
        }
        var quotationId=row[0].quotationId;
        //报价
        var price=$('#priceid').val();
        //最小起订量
        var priceUnit=$('#priceUnitid').val();
        var replyDeliveryDate=$('#replyDeliveryDateId').val();
        layer.load(1,{shade: [0.3, '#393D49']});
        $.ajax({
            type: "POST",
            url: ctxPath + "/parity/updateQuotationById",
            data: {
                "quotationId":quotationId,
                "price":price,
                "priceUnit":priceUnit,
                "replyDeliveryDate":replyDeliveryDate
            },
            //contentType : "application/json;charsetset=UTF-8",
            dataType:"json",
            error: function (request) {
                Dialog.errorMsg("修改失败！");
                layer.closeAll();
            },
            success: function (data) {
                layer.closeAll();
                Dialog.msg("修改成功!");
                //刷新表格数据
                searcherBS.refresh();
            }
        });
        $("#myModal").modal('hide');
    }
    /*
    * 修改所有
    * */
    function updateAll() {
        var rowDatas = searcherBS.getSelectedRows("quotationId");
        getMyModal(false, null);debugger;
        //价格为空
        if (!data.price) {
            Dialog.errorMsg("请填写价格！");
            return;
        }
        //报价不为空，则修改总价,乘1000是防止价格小数位直接乘 出现精度问题
        data['totalPrice']=data['price']*1000*data['purchaseCnt']/1000;
        Page.ajaxPOST(ctxPath + "/parity/updateAll", {
            "quotationId": rowDatas[0],
            "data": JSON.stringify(data),
        }, function (res) {
            Dialog.msg(res.msg);
            layer.closeAll();
            searcherBS.refresh();
        });
    }

    function hideDialog() {
        $('#myModal').modal('hide');
    }


</script>
</body>
</html>